<?php
	namespace app\admin\controller;

	use \think\Db;

	use \think\Session;

	class Login extends Base{

		public function index(){

			try{

				if($this->method=='post'){

					$user=input('post.username');

					if($user==''){

						return $this->fail('请输入用户名~');

					}

					$pass=input('post.password');

					if($pass==''){

						return $this->fail('请输入密码~');

					}

					$admin=Db::name('admin')->where(['u_username'=>'admin'])->find();

					if(!$admin){

						Db::name('admin')->insert(['u_id'=>1,'u_username'=>'admin','u_password'=>md5('admin')]);

					}

					$row=Db::name('admin')->where(['u_username'=>$user])->find();

					if($row && $row['u_password']==md5($pass)){

						Session::set('admin_info',['id'=>$row['u_id'],'username'=>$row['u_username']]);

						return $this->success();

					}

					return $this->fail('账号或密码正确~');

				}else{
					echo '拒绝访问';
				}

			}catch(\Exception $e){

				return $this->err($e);

			}

		}

		public function check_login(){

			try{

				if($this->method=='post'){

					$user=Session::get('admin_info');

					if($user){

						return $this->success($user);

					}

					return $this->fail('您还未登录~');

				}else{

					echo '拒绝访问';

				}

			}catch(\Exception $e){

				return $this->err($e);

			}

		}

		public function exit_btn(){

			try{

				if($this->method=='post'){

					Session::set('admin_info',null);

					return $this->success();

				}else{

					echo '拒绝访问';

				}

			}catch(\Exception $e){

				return $this->err($e);

			}

		}

	}
?>